package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.kitchen.KitchenCooperation;
import org.dromara.kitchen.service.IKitchenCooperationService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 加盟合作留言信息
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/cooperation")
public class KitchenCooperationController extends BaseController {

    private final IKitchenCooperationService kitchenCooperationService;

    /**
     * 查询加盟合作留言信息列表
     */
    @SaCheckPermission("kitchen:cooperation:list")
    @GetMapping("/list")
    public TableDataInfo<KitchenCooperation> list(KitchenCooperation kitchenCooperation, PageQuery pageQuery) {
        QueryWrapper<KitchenCooperation> qw = new QueryWrapper<>();

        TableDataInfo<KitchenCooperation> td = PageUtils.buildDataInfo(kitchenCooperationService.page(PageUtils.buildPage(),qw));
        List<KitchenCooperation> list = td.getRows();
        return td;
    }

    /**
     * 导出加盟合作留言信息列表
     */
    @SaCheckPermission("kitchen:cooperation:export")
    @Log(title = "加盟合作留言信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KitchenCooperation kitchenCooperation, HttpServletResponse response) {
        List<KitchenCooperation> list = kitchenCooperationService.list();
        ExcelUtil.exportExcel(list, "加盟合作留言信息", KitchenCooperation.class, response);
    }

    /**
     * 获取加盟合作留言信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:cooperation:query")
    @GetMapping("/{id}")
    public R<KitchenCooperation> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(kitchenCooperationService.getById(id));
    }

    /**
     * 新增加盟合作留言信息
     */
    @SaCheckPermission("kitchen:cooperation:add")
    @Log(title = "加盟合作留言信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KitchenCooperation kitchenCooperation) {
        return toAjax(kitchenCooperationService.save(kitchenCooperation));
    }

    /**
     * 修改加盟合作留言信息
     */
    @SaCheckPermission("kitchen:cooperation:edit")
    @Log(title = "加盟合作留言信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KitchenCooperation kitchenCooperation) {
        return toAjax(kitchenCooperationService.updateById(kitchenCooperation));
    }

    /**
     * 删除加盟合作留言信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:cooperation:remove")
    @Log(title = "加盟合作留言信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(kitchenCooperationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
